﻿using Microsoft.AspNetCore.Mvc;
using ServiceCore.Entity.Core;
using ServiceCore.Entity.Model;
using ServiceCore.Entity.Param.User;
using ServiceCore.Service;
using ServiceCore.WebApi.jwt;

namespace ServiceCore.WebApi.Controllers
{
    /// <summary>
    /// 用户
    /// </summary>
    [Route("Api/[controller]/[action]")]
    [ApiController]
    public class UserController : ApiJwtController
    {
        private UserInfoService userService = new UserInfoService();

        /// <summary>
        /// 列表
        /// </summary>
        /// <arg name="arg"></arg>
        /// <returns></returns>
        [HttpPost]
        public PageParam<UserInfo, List<UserInfo>> PageList(PageParam<UserInfo, List<UserInfo>> arg)
        {
            arg = userService.User_PageList(arg);

            return arg;
        }

        /// <summary>
        /// 单个
        /// </summary>
        /// <arg name="arg"></arg>
        /// <returns></returns>
        [HttpPost]
        public BaseParam<UserInfo, UserInfo> GetModel(BaseParam<UserInfo, UserInfo> arg)
        {
            arg.Data = userService.GetById(arg.Id);
            if (arg.Data != null)
                arg.Data.Pwd = "";

            return arg;
        }


        /// <summary>
        /// 新增
        /// </summary>
        /// <arg name="arg"></arg>
        /// <returns></returns>
        [HttpPost]
        public BaseParam<UserInfo, long> Add(BaseParam<UserInfo, long> arg)
        {
            arg = userService.User_Add(arg);

            return arg;
        }

        /// <summary>
        /// 编辑
        /// </summary>
        /// <arg name="arg"></arg>
        /// <returns></returns>
        [HttpPost]
        public BaseParam<UserInfo, long> Edit(BaseParam<UserInfo, long> arg)
        {
            arg = userService.User_Edit(arg);

            return arg;
        }

        /// <summary>
        /// 修改密码
        /// </summary>
        /// <arg name="arg"></arg>
        /// <returns></returns>
        [HttpPost]
        public BaseParam<UserChangePwdParam, long> ChangePwd(BaseParam<UserChangePwdParam, long> arg)
        {
            arg = userService.User_ChangePwd(arg);

            return arg;
        }

        /// <summary>
        /// 是否启用
        /// </summary>
        /// <arg name="arg"></arg>
        /// <returns></returns>
        [HttpPost]
        public BaseParam<UserInfo, long> Used(BaseParam<UserInfo, long> arg)
        {
            arg = userService.User_Used(arg);

            return arg;
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <arg name="arg"></arg>
        /// <returns></returns>
        [HttpPost]
        public BaseParam<UserInfo, long> Del(BaseParam<UserInfo, long> arg)
        {
            if (arg.Id == 1 || arg.Id == 2)
            {
                arg.ErrorMsg = "权限不足";
                return arg;
            }
            var userInfo = GetJwtUserInfo();
            if (userInfo?.Id == arg.Id)
            {
                arg.ErrorMsg = "无操作权限";
                return arg;
            }
            if (userInfo?.IsAdmin != 1)
            {
                arg.ErrorMsg = "权限不足";
                return arg;
            }
            arg = userService.User_Delete(arg);

            return arg;
        }

        /// <summary>
        /// 退出登录
        /// </summary>
        /// <param name="arg"></param>
        /// <returns></returns>        
        [HttpPost]
        public BaseParam<UserInfo, long> LoginOut(BaseParam<UserInfo, long> arg)
        {
            arg = userService.User_LoginOut_Verify(arg);
            if (string.IsNullOrEmpty(arg.ErrorMsg) == false)
                return arg;

            //强制登出
            JwtUserFileService.RevokeRefreshToken(arg.Id.ToString());

            return arg;
        }
    }
}
